package com.tikeyc.flutterandroid.custom;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StringCodec;

/*BasicMessageChannel通信
 *BasicMessageChannel用于Native和Flutter互相发送消息，一方给另一方发送消息，收到消息之后给出回复
* */
public class MyBasicMessageChannel {
    private static String Basic_Method_Channel = "com.tikeyc.fluttermodule/basicMethodChannel";

    public Context context;
    public BasicMessageChannel basicMethodChannel;

    public MyBasicMessageChannel(Context context, BasicMessageChannel basicMethodChannel) {
        this.context = context;
        this.basicMethodChannel = basicMethodChannel;
    }

    public static MyBasicMessageChannel instance(Context context, FlutterEngine flutterEngine) {
        BasicMessageChannel basicMethodChannel = new BasicMessageChannel(flutterEngine.getDartExecutor(), Basic_Method_Channel, StringCodec.INSTANCE);
        return new MyBasicMessageChannel(context, basicMethodChannel);
    }

    public static MyBasicMessageChannel instance(Context context, FlutterEngine flutterEngine, MessageCodec codec) {
        BasicMessageChannel basicMethodChannel = new BasicMessageChannel(flutterEngine.getDartExecutor(), Basic_Method_Channel, codec);
        return new MyBasicMessageChannel(context, basicMethodChannel);
    }

    public static MyBasicMessageChannel instance(Context context, FlutterEngine flutterEngine, String name) {
        BasicMessageChannel basicMethodChannel = new BasicMessageChannel(flutterEngine.getDartExecutor(), name, StringCodec.INSTANCE);
        return new MyBasicMessageChannel(context, basicMethodChannel);
    }

    public static MyBasicMessageChannel instance(Context context, FlutterEngine flutterEngine, String name, MessageCodec codec) {
        BasicMessageChannel basicMethodChannel = new BasicMessageChannel(flutterEngine.getDartExecutor(), name, codec);
        return new MyBasicMessageChannel(context, basicMethodChannel);
    }

    public void setMessageHandler() {
        basicMethodChannel.setMessageHandler(new BasicMessageChannel.MessageHandler() {
            @Override
            public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply reply) {
                Toast.makeText(context, message.toString(), Toast.LENGTH_SHORT).show();
                Log.v("test", "收到了Flutter端发来的消息：" + message);
                reply.reply("android tikeyc");
            }
        });
    }

    public void setMessageHandler(BasicMessageChannel.MessageHandler messageHandler) {
        basicMethodChannel.setMessageHandler(messageHandler);
    }

    /*发送的消息会以二进制的形式进行处理，所以要针对不同类型的数据进行二进制编码
        编码类型	消息格式
        BinaryCodec	发送二进制消息
        JSONMessageCodec	发送Json格式消息
        StandardMessageCodec	发送基本型数据
        StringCodec	发送String类型消息向Flutter端发送消息
   basicMethodChannel.send("Hello, this is BasicMethodChannel Send msg!", new BasicMessageChannel.Reply() {
        @Override
        public void reply(@Nullable Object reply) {
            Log.i("test", "收到了Flutter端发来的回复：$replyString");
        }
    });
    * */
    public void send(String message, BasicMessageChannel.Reply callback) {
        basicMethodChannel.send(message, callback);
    }
}
